package com.xpgk.mvc.budgetMonth;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.productSet.Product;
import com.xpgk.tools.ToolRandoms;

import net.sf.json.JSONObject;

@Service(name = BudgetMonthService.serviceName)
public class BudgetMonthService extends  BaseService{
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BudgetMonthService.class);
	public static final String serviceName = "budgetMonthService";
	
	
	public  List<Map>  getBudgetMonth(String organIds)
	{	
		List<Map> budgetMonthList = new ArrayList<>();
		   Calendar cal = Calendar.getInstance();
		   int year = cal.get(Calendar.YEAR);
		   int month = cal.get(Calendar.MONTH) + 1;
		   
		   for(int i=(month+1);i>0;i--)
		   {
			   Map<String, String> map = new HashMap<>();
			   map.put("month", i+"");
			   String yearmonth=year+"-"+String.format("%02d", i);
			   String ccMonth="%"+year+"-"+String.format("%02d", i)+"%";
			   //获取疗程实际
			   Customer  cus= BudgetMonth.dao.getCustomerAndLcnum(Code.lc_ids,Code.dlc_units,organIds,ccMonth);
			   map.put("lc_newcus_sjnum", cus.getLong("count").toString());
			   map.put("lc_newcus_sjlcnum", cus.getBigDecimal("lcsum").toString());
			   //获取疗程计划预算
			   Map<String, Object> queryParam = new HashMap<String, Object>();
				queryParam.put("yearmonth", yearmonth);
				queryParam.put("organIds", organIds);
				queryParam.put("productIds", Code.lc_ids);
				queryParam.put("check_state", "1");

				List<Record> lis = find(BudgetMonth.sqlId_getlcBudgetMonth, queryParam);
			  
			   if(lis!=null && lis.size()>0)
			   {	
//				   BudgetMonth  bm=BudgetMonth.dao.findById(lis.get(0).getStr(BudgetMonth.column_ids));
//				   BudgetMonthDetail  bmd=BudgetMonthDetail.dao.findById(lis.get(0).getStr("bmdids"));
//				   map.put("lc_newcus_jhnum", bmd.getFloat("newnum")==null?0+"":bmd.getFloat("newnum").toString());
//				   map.put("lc_newcus_jhlcnum", bmd.getFloat("newlcnum")==null?0+"":bmd.getFloat("newlcnum").toString());
				   map.put("lc_newcus_jhnum", lis.get(0).getFloat("newnum")==null?0+"":lis.get(0).getFloat("newnum").toString());
				   map.put("lc_newcus_jhlcnum", lis.get(0).getFloat("newlcnum")==null?0+"":lis.get(0).getFloat("newlcnum").toString());
			   }else
			   {
				   map.put("lc_newcus_jhnum", 0+"");
				   map.put("lc_newcus_jhlcnum",0+"");
			   }
			  
			   //获取各个产品实际新老顾客数量和金额
			   List<Product> pList=BudgetMonth.dao.getNumAndPrice(ccMonth, yearmonth, organIds, Code.lc_ids);
			   if(pList!=null && pList.size()>0)
			   {
				   for(int j=0;j<pList.size();j++)
				   {
					   map.put("pro_"+pList.get(j).getStr("proIds")+"_sjnum_newcus",pList.get(j).getBigDecimal("newnum")==null?0+"":pList.get(j).getBigDecimal("newnum").toString());
					   map.put("pro_"+pList.get(j).getStr("proIds")+"_sjnum_oldcus",(pList.get(j).getBigDecimal("oldnum")==null?0+"":pList.get(j).getBigDecimal("oldnum").toString()));
					   map.put("pro_"+pList.get(j).getStr("proIds")+"_sjprice_newcus",pList.get(j).getDouble("newprice")==null?0+"":pList.get(j).getDouble("newprice").toString());
					   map.put("pro_"+pList.get(j).getStr("proIds")+"_sjprice_oldcus",pList.get(j).getDouble("oldprice")==null?0+"":pList.get(j).getDouble("oldprice").toString());
				   }
			   }
			   //获取各个产品计划新老顾客数量和金额
			    Map<String, Object> queryParam1 = new HashMap<String, Object>();
			   	queryParam1.put("organIds", organIds);
				queryParam1.put("yearmonth", yearmonth);
				queryParam1.put("check_state", "1");
				queryParam1.put("typeIds", Code.lc_ids);
				List<Record> lis1 = find(BudgetMonth.sqlId_getProBudgetMonth, queryParam);
				if(lis1!=null && lis1.size()>0)
				{
					for(int m=0;m<lis1.size();m++)
					{
						map.put("pro_"+lis1.get(m).getStr("proIds")+"_jhnum_newcus",lis1.get(m).getFloat("newnum")==null?0+"":lis1.get(m).getFloat("newnum").toString());
						map.put("pro_"+lis1.get(m).getStr("proIds")+"_jhnum_oldcus",(lis1.get(m).getFloat("oldnum")==null?0+"":lis1.get(m).getFloat("oldnum").toString()));
						map.put("pro_"+lis1.get(m).getStr("proIds")+"_jhprice_newcus",lis1.get(m).getFloat("newprice")==null?0+"":lis1.get(m).getFloat("newprice").toString());
						map.put("pro_"+lis1.get(m).getStr("proIds")+"_jhprice_oldcus",lis1.get(m).getFloat("oldprice")==null?0+"":lis1.get(m).getFloat("oldprice").toString());
					}
				}
			   budgetMonthList.add(map);
		   }
	return  budgetMonthList;
	}
	
	
	/**保存月预算
	 * 
	 * @param budgetJson
	 * @param organIds
	 * @return
	 */
	public  String   saveBudget(String budgetJson,String organIds,String userIds)
	{
		JSONObject jsonObject=JSONObject.fromObject(budgetJson);
		 Calendar cal = Calendar.getInstance();
		   int year = cal.get(Calendar.YEAR);
		   int month = cal.get(Calendar.MONTH) + 1;
		   String yearmonth=year+"-"+String.format("%02d", month+1);
		//查出除疗程外产品
		   String sql=getSqlMy(Product.sqlId_nolcpro);
		List<Product>  pList=Product.dao.find(sql,Code.lc_ids);
		
		BudgetMonth  bm=BudgetMonth.dao.getBudgetMonth(organIds, yearmonth);
		  if(bm!=null)
		  {
			  //更新月预算
			  bm.set(BudgetMonth.column_changeUserIds, userIds)
			  	.set(BudgetMonth.column_changeDate, new Date())
			  	.set(BudgetMonth.column_type, "1")
			  	.set(BudgetMonth.column_check_state, "2")
			  	.set(BudgetMonth.column_gxsj, new Date());
			  if(!bm.update())
			  {
				  log.debug("更新月预算记录失败");
				  throw new RuntimeException("更新月预算记录失败!");
			  }
			  //更新疗程预算
			  //获取疗程计划预算
			  BudgetMonthDetail  bmd=BudgetMonthDetail.dao.getBudgetMonthDetail(bm.getStr(BudgetMonth.column_ids), Code.lc_ids);
			  bmd.set(BudgetMonthDetail.column_newCusPlanNum, jsonObject.get("lc_newcus_jhnum"))
			  		.set(BudgetMonthDetail.column_newCusLcNum, jsonObject.get("lc_newcus_jhlcnum"))
			  		.set(BudgetMonthDetail.column_gxsj, new Date());
			  if(!bmd.update())
			  {
				  log.debug("更新疗程月预算记录失败");
				  throw new RuntimeException("更新疗程月预算记录失败!");
			  }
			  //更新其他产品
			  if(pList!=null && pList.size()>0)
			  {
				  for(Product p:pList)
				  {
					  BudgetMonthDetail  probmd=BudgetMonthDetail.dao.getBudgetMonthDetail(bm.getStr(BudgetMonth.column_ids), p.getStr(Product.column_ids));
					  probmd.set(BudgetMonthDetail.column_newCusPlanNum, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhnum_newcus"))
					  		.set(BudgetMonthDetail.column_newCusPlanPrice, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhprice_newcus"))
					  		.set(BudgetMonthDetail.column_oldCusPlanNum, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhnum_oldcus"))
					  		.set(BudgetMonthDetail.column_oldCusPlanPrice, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhprice_oldcus"))
					  		.set(BudgetMonthDetail.column_gxsj, new Date());
					  if(!probmd.update())
					  {
						  log.debug("更新"+p.getStr(Product.column_name)+"月预算记录失败");
						  throw new RuntimeException("更新"+p.getStr(Product.column_name)+"月预算记录失败");
					  }
					  		
				  }
			  }
			  
		  }
		  else
		  {
			  //保存月预算
			  BudgetMonth  ybm = new BudgetMonth();
			  ybm.set(BudgetMonth.column_ids, ToolRandoms.getUuid(true))
			  		.set(BudgetMonth.column_organIds, organIds)
			  		.set(BudgetMonth.column_yearmonth, yearmonth)
			  		.set(BudgetMonth.column_submitUserIds, userIds)
			  		.set(BudgetMonth.column_submitDate, new Date())
			  		.set(BudgetMonth.column_submit_state, "2")
			  		.set(BudgetMonth.column_type, "0")
			  		.set(BudgetMonth.column_check_state, "0")
			  		.set(BudgetMonth.column_gxsj, new Date());
			  if(!ybm.save())
			  {
				  log.debug("保存月预算记录失败");
				  throw new RuntimeException("保存月预算记录失败");
			  }
			  //保存疗程月预算
			  BudgetMonthDetail  lcbmd = new BudgetMonthDetail();
			  lcbmd.set(BudgetMonthDetail.column_ids, ToolRandoms.getUuid(true))
			  		.set(BudgetMonthDetail.column_budgetMonthIds, ybm.getStr(BudgetMonth.column_ids))
			  		.set(BudgetMonthDetail.column_productIds, Code.lc_ids)
			  		.set(BudgetMonthDetail.column_newCusPlanNum,jsonObject.get("lc_newcus_jhnum"))
			  		.set(BudgetMonthDetail.column_newCusLcNum, jsonObject.get("lc_newcus_jhlcnum"))
			  		.set(BudgetMonthDetail.column_gxsj, new Date());
			  if(!lcbmd.save())
			  {
				  log.debug("保存疗程月预算记录失败");
				  throw new RuntimeException("保存疗程月预算记录失败");
			  }
			  //保存除疗程外产品月预算
			  if(pList!=null && pList.size()>0)
			  {
				  for(Product p:pList)
				  {		
					  BudgetMonthDetail  pbmd = new BudgetMonthDetail();
					  pbmd.set(BudgetMonthDetail.column_ids,ToolRandoms.getUuid(true))
					  	.set(BudgetMonthDetail.column_budgetMonthIds, ybm.getStr(BudgetMonth.column_ids))
					  	.set(BudgetMonthDetail.column_productIds, p.getStr(Product.column_ids))
					    .set(BudgetMonthDetail.column_newCusPlanNum, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhnum_newcus"))
				  		.set(BudgetMonthDetail.column_newCusPlanPrice, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhprice_newcus"))
				  		.set(BudgetMonthDetail.column_oldCusPlanNum, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhnum_oldcus"))
				  		.set(BudgetMonthDetail.column_oldCusPlanPrice, jsonObject.get("pro_"+p.getStr(Product.column_ids)+"_jhprice_oldcus"))
				  		.set(BudgetMonthDetail.column_gxsj, new Date());
					  if(!pbmd.save())
					  {
						  log.debug("保存"+p.getStr(Product.column_name)+"月预算记录失败");
						  throw new RuntimeException("保存"+p.getStr(Product.column_name)+"月预算记录失败");
					  }
				  }
			  }
		  }
		   
		   return ConstantRender.render_success_code;
	}
}
